'use strict'
const express=require('express')
const cors=require('cors')
const app=express();
const bodyParser=require('body-parser')
const pg=require('pg')
const router=express.Router();
const option={
  user:'liuxintong',
  password:'285023liu',
  database:'xinyou',
  //port:'5990'
}
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:false}))
app.listen(5990,()=>console.log('启动服务'))

const pool=new pg.Pool(option)
function Result({code=1,msg='',data={}}){
  this.code=code;
  this.msg=msg;
  this.data=data;
}

//登录拦截
/*
let login=true
app.all('*',(req,res)=>{
  if(!login) return res.json('未登录');
  next();
})
*/
let data=[];
app.get('/timeletter',(req,res)=>{
  //return res.json('hello')
  //return res.json({query:req.query,data:req.params,json:req.body})
  //let sql='select * from timeletter'
  //conn.query('select * from timeletter',(e,r)=>res.json(new Result({data:r})))
  //let data=[]
  pool.connect((error,client,done)=>{
      let sql='select * from timeletter'
      client.query(sql,[],(e,r)=>{
        done();
        r.rows.map((item,idx)=>{
          data.push(item)
        })
        res.json(new Result({data:r.rows}))
      })
  })
})
